package com.walker.rest;

import java.util.Map;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.walker.service.meta.ExternalRestServ;
import com.walker.util.CommonUtils;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("/rest/meta")
public class ExternalRestMetaController {
	
	@Resource
	private ExternalRestServ serv;
	
	@SuppressWarnings("unchecked")
	@PostMapping("tabInfo")
	public String tabInfo(@RequestBody String request) throws Exception {
		Map<String, Object> params = JSONObject.fromObject(request);
		JSONObject json = new JSONObject();
		try {
			if ("".equals(CommonUtils.valueOf(params.get("ds_id"))) || "".equals(CommonUtils.valueOf(params.get("db_code")))) {
				json.put("code", 500);
				json.put("msg", "ds_id or db_code not is null");
				return json.toString();
			}
			json.put("data", serv.getTableInfo(params));
			json.put("flag", true);
			json.put("code", 200);
		} catch (Exception e) {
			json.put("code", 500);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@SuppressWarnings("unchecked")
	@PostMapping("colInfo")
	public String colInfo(@RequestBody String request) throws Exception {
		Map<String, Object> params = JSONObject.fromObject(request);
		JSONObject json = new JSONObject();
		try {
			if ("".equals(CommonUtils.valueOf(params.get("table_id")))) {
				json.put("code", 500);
				json.put("msg", "table_id not is null");
				return json.toString();
			}
			json.put("data", serv.getColumnInfo(params));
			json.put("flag", true);
			json.put("code", 200);
		} catch (Exception e) {
			json.put("code", 500);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping("prtInfo")
	public String prtInfo(@RequestBody String request) throws Exception {
		Map<String, Object> params = JSONObject.fromObject(request);
		JSONObject json = new JSONObject();
		try {
			if ("".equals(CommonUtils.valueOf(params.get("table_id")))) {
				json.put("code", 500);
				json.put("msg", "table_id not is null");
				return json.toString();
			}
			json.put("data", serv.getPartitionInfo(params));
			json.put("flag", true);
			json.put("code", 200);
		} catch (Exception e) {
			json.put("code", 500);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}
	
}
